package com.campus.web.control;

import com.campus.model.Activity;
import com.campus.service.ActivityService;
import com.campus.web.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author HLQ
 * @date 2024/11/23 0:58
 */
@Controller
@RequestMapping("/activities")
@Slf4j
@Validated
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @PostMapping
    @ResponseBody
    public ResponseResult<String> addActivity(@RequestBody @Validated Activity activity) {
        log.info("activity: {}", activity);
        activityService.createActivity(activity);
        return ResponseResult.ok("添加成功");
    }


    @PostMapping("/update")
    @ResponseBody
    public ResponseResult<String> updateActivityById(@RequestParam @NotBlank  int id, @RequestBody @Validated Activity activity) {
        log.info("activity: {}, id: {}", activity, id);
        activity.setId(id);  // 确保活动的 ID 被正确设置
        activityService.updateActivityById(activity);
        return ResponseResult.ok("更新成功");
    }

    @PostMapping("/publish")
    @ResponseBody
    public ResponseResult<String> publishActivityById(@RequestParam @NotBlank  int id,
                                                      @RequestParam @NotBlank int status) {
        log.info("id: {}, status: {}", id, status);
        activityService.publishActivityById(id, status);
        return ResponseResult.ok("发布成功");
    }

    @GetMapping
    @ResponseBody
    public ResponseResult<List<Activity>> queryActivityByStatus(@RequestParam @NotBlank int status) {
        log.info("查询活动状态: {}", status);
        List<Activity> activities = activityService.queryActivityByStatus(status);
        return ResponseResult.ok(activities, "状态查询成功");
    }

    @GetMapping("/search")
    @ResponseBody
    public ResponseResult<List<Activity>> fuzzyMatchingActivityByName(@RequestParam @NotBlank String name) {
        log.info("活动名称查询: {}", name);
        List<Activity> activities = activityService.fuzzyMatchingActivityByName(name);
        return ResponseResult.ok(activities, "名称查询成功");
    }
}
